package com.example.admin.testdemo.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.admin.testdemo.R;
import com.example.admin.testdemo.dao.DataHandler;
import com.example.admin.testdemo.helper.DataHandle;
import com.example.admin.testdemo.rs232.Command;
import com.example.admin.testdemo.rs232.Serial;
import com.example.admin.testdemo.util.ByteUtil;
import com.example.admin.testdemo.util.WeightCmd;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class WeightShopActivity extends BaseActivity implements View.OnClickListener {
    private static String TAG = "WeightShopActivity";
    private EditText googs1_weight;
    private EditText googs2_weight;
    private EditText googs3_weight;
    private EditText googs4_weight;
    private EditText googs1_c;
    private EditText googs2_c;
    private EditText googs3_c;
    private EditText googs4_c;
    private EditText googs1_s;
    private EditText googs2_s;
    private EditText googs3_s;
    private EditText googs4_s;
    private EditText googs_take;
    private TextView googs_total;
    private TextView googs_count_1;
    private TextView googs_count_2;
    private TextView googs_count_3;
    private TextView googs_count_4;
    private Button commit;
    private Button commit_lx;
    private boolean lxFlag = false;
    private long wdTime=0;
    private ScheduledExecutorService scheduledThreadPool;
    private Serial serial = Serial.getSerialInstance();
    private DataHandle dataHandle;
    private int[] goodsStd={0,0,0,0};
    private int[] goodsoffset={0,0,0,0};
    private int srcTotal=0;
    private boolean isFirst=false;
    private List<TextView> tvList=new ArrayList<>();
    private Serial.DataCallback callback = new Serial.DataCallback() {
        @Override
        public void onDataReceived(byte[] buffer, int size) {
//            Log.e(TAG, "接收：" + Command.printByteInfo(buffer,size));
            if (WeightCmd.cmdFH(buffer)) {
                if(buffer[6]==(byte)0x01&&isFirst){
                    srcTotal=WeightCmd.weight(buffer);
                    isFirst=false;
                }
                test(srcTotal-WeightCmd.weight(buffer));
                setTv(googs_total, Integer.toString(WeightCmd.weight(buffer)));
                Log.e(TAG, Command.printByteInfo(buffer,size)+"---接收重量：" + WeightCmd.weight(buffer));
            }else {
                Log.e(TAG, "接收：" + Command.printByteInfo(buffer,size));
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weight_shop);
        serial.setCallback(callback);
        initTimer();
        initView();
        initData();
        dataHandle=new DataHandle();
    }

    private void initTimer() {
        if (scheduledThreadPool == null) {
            scheduledThreadPool = new ScheduledThreadPoolExecutor(1);
        }
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (lxFlag) {
                    sendData(1);
                }
            }
        }, 0, 1000, TimeUnit.MILLISECONDS);

    }

    private void initData() {
    }

    private void initWeight(){
        goodsStd[0]=getEtNum(googs4_weight);
        goodsStd[1]=getEtNum(googs3_weight);
        goodsStd[2]=getEtNum(googs2_weight);
        goodsStd[3]=getEtNum(googs1_weight);
        goodsoffset[0]=getEtNum(googs4_c);
        goodsoffset[1]=getEtNum(googs3_c);
        goodsoffset[2]=getEtNum(googs2_c);
        goodsoffset[3]=getEtNum(googs1_c);
    }

    private void initView() {
        googs1_weight = findViewById(R.id.googs1_weight);
        googs2_weight = findViewById(R.id.googs2_weight);
        googs3_weight = findViewById(R.id.googs3_weight);
        googs4_weight = findViewById(R.id.googs4_weight);
        googs1_c = findViewById(R.id.googs1_c);
        googs2_c = findViewById(R.id.googs2_c);
        googs3_c = findViewById(R.id.googs3_c);
        googs4_c = findViewById(R.id.googs4_c);
        googs1_s = findViewById(R.id.googs1_s);
        googs2_s = findViewById(R.id.googs2_s);
        googs3_s = findViewById(R.id.googs3_s);
        googs4_s = findViewById(R.id.googs4_s);
        googs_count_1 = findViewById(R.id.googs_count_1);
        googs_count_2 = findViewById(R.id.googs_count_2);
        googs_count_3 = findViewById(R.id.googs_count_3);
        googs_count_4 = findViewById(R.id.googs_count_4);
        googs_total = findViewById(R.id.googs_total);
        googs_take = findViewById(R.id.googs_take);
        commit = findViewById(R.id.commit);
        commit.setOnClickListener(this);
        commit_lx = findViewById(R.id.commit_lx);
        commit_lx.setOnClickListener(this);
        tvList.add(googs_count_1);
        tvList.add(googs_count_2);
        tvList.add(googs_count_3);
        tvList.add(googs_count_4);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.commit) {
            test(getEtNum(googs_take));
        } else if (v.getId() == R.id.commit_lx) {
            lxcs();
        }

    }

    private void test(int weight){
        initWeight();
        int[] temp=dataHandle.checkWhichOne(weight,goodsStd,goodsoffset);
        for (int i = 0; i <temp.length ; i++) {
            setTv(tvList.get(temp.length-i-1),Integer.toString(temp[i]));
            Log.e(TAG,"商品"+Integer.toString(temp.length-i-1)+":"+temp[i]+"个");
        }
    }

    private void lxcs() {
        if (lxFlag) {
            lxFlag = false;
            isFirst=false;
            commit_lx.setText("数据读取");
        } else {
            lxFlag = true;
            isFirst=true;
            commit_lx.setText("数据读取中");
        }
    }


    private void sendData(int add) {
        if (serial != null) {
            setTv(googs_total, "0");
            byte[] temp = WeightCmd.getCdmData(1);
            Log.e(TAG,"发送："+Command.printByteInfo(temp));
            serial.sendData(temp);
        }

    }


    private int totalGoogs() {
        int w1 = getWeight(googs1_weight, googs1_c, googs1_s);
        int w2 = getWeight(googs2_weight, googs2_c, googs2_s);
        int w3 = getWeight(googs3_weight, googs3_c, googs3_s);
        int w4 = getWeight(googs4_weight, googs4_c, googs4_s);
        int total = w1 + w2 + w3 + w4;
        googs_total.setText("总重量：" + Integer.toString(total));
        return total;
    }

    private int getWeight(EditText w, EditText c, EditText s) {
        int temp = 0;
        if (getEtNum(s) == 0) {
            return 0;
        }
        temp = getEtNum(w) * getEtNum(s) + (DataHandler.getRandom(getEtNum(c)) - DataHandler.getRandom(getEtNum(c)));
        return temp;
    }

    private int getEtNum(EditText w) {
        int temp = 0;
        String tt = w.getText().toString();
        if (!TextUtils.isEmpty(tt)) {
            temp = Integer.parseInt(tt);
        }
        return temp;
    }

    private void setTv(final TextView tv, final String data) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv.setText( data);
            }
        });
    }
}
